package com.cc.openeye.dynamic.recommend.items.loadMore

import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.rememberUpdatedState
import com.cc.openeye.dynamic.recommend.entity.LoadMoreStatus
import com.tencent.kuikly.compose.foundation.clickable
import com.tencent.kuikly.compose.foundation.layout.Box
import com.tencent.kuikly.compose.foundation.layout.Row
import com.tencent.kuikly.compose.foundation.layout.fillMaxWidth
import com.tencent.kuikly.compose.foundation.layout.height
import com.tencent.kuikly.compose.foundation.layout.padding
import com.tencent.kuikly.compose.foundation.layout.width
import com.tencent.kuikly.compose.material3.CircularProgressIndicator
import com.tencent.kuikly.compose.material3.MaterialTheme
import com.tencent.kuikly.compose.material3.Text
import com.tencent.kuikly.compose.ui.Alignment
import com.tencent.kuikly.compose.ui.Modifier
import com.tencent.kuikly.compose.ui.unit.dp
import kotlinx.coroutines.launch

@Composable
fun LoadMoreComponent(loadMoreStatus: LoadMoreStatus, onLoadMore: () -> Unit) {

    val currOnLoadMore = rememberUpdatedState(onLoadMore)

    LaunchedEffect(true) {
        currOnLoadMore.value.invoke()
    }

    val rememberCoroutineScope = rememberCoroutineScope()

    Box(modifier = Modifier.fillMaxWidth().height(50.dp), contentAlignment = Alignment.Center) {

        when (loadMoreStatus) {
            LoadMoreStatus.HaveMore -> {
                Row(verticalAlignment = Alignment.CenterVertically) {
                    CircularProgressIndicator(
                        modifier = Modifier.width(15.dp).height(15.dp),
                        color = MaterialTheme.colorScheme.secondary,
                        trackColor = MaterialTheme.colorScheme.surfaceVariant,
                        strokeWidth = 3.dp
                    )
                    Text("加载中", modifier = Modifier.padding(start = 5.dp))
                }
            }

            LoadMoreStatus.NoMore -> {
                Text("-已经没有更多数据-")
            }

            LoadMoreStatus.LoadErr -> {
                Text("-加载异常，请点击重试-", modifier = Modifier.clickable {
                    rememberCoroutineScope.launch {
                        currOnLoadMore.value.invoke()
                    }
                })
            }

            LoadMoreStatus.NONE -> {}
        }

    }
}